package siscom.componentes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionListener;

import siscom.modelos.Comanda;

public class SisListComandas extends JPanel{
	/**
	 * Author Carlos Ribeiro
	 */
	private static final long serialVersionUID = 5892526733579263290L;
	private Modelo modelo = new Modelo();
	private int numeroComandas = 0;
	private JList lista = new JList() {
		public String getToolTipText(java.awt.event.MouseEvent event) {
			int indice = locationToIndex(event.getPoint());
			if (indice != -1) {
				Comanda comanda = (Comanda) getModel().getElementAt(indice);
				if (!comanda.isAberta()) {
					return comanda.getNumero() + ": Comanda fechada";
				}
				return comanda.getNumero() + ": Comanda aberta";
			}
			return "";
		};
	};
	private JScrollPane scrollPane;
	int lastComanda = 0;
	public SisListComandas() {
		setLayout(new BorderLayout(0, 0));
		getLista().setLayoutOrientation(JList.HORIZONTAL_WRAP);
		getLista().setFixedCellWidth(50);
		getLista().setFixedCellHeight(70);
		getLista().setVisibleRowCount(-1);
		getLista().setCellRenderer(new Render());
		getLista().setModel(modelo);
		scrollPane = new JScrollPane(getLista());
		scrollPane.setAlignmentX(JFrame.LEFT_ALIGNMENT);
		add(scrollPane);
		lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//		lista.addKeyListener(new KeyListener() {
//			
//			@Override
//			public void keyTyped(KeyEvent e) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//			@Override
//			public void keyReleased(KeyEvent e) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//			@Override
//			public void keyPressed(KeyEvent e) {
//				// TODO Auto-generated method stub
//				System.out.println("keypressed");
//			}
//		});
	}
	public SisListComandas(int numeroComandas) {
		this();
		this.numeroComandas = numeroComandas;
		limpa();
	}
	public Comanda getComanda(String numero) {
		for(int i=0; i<modelo.getSize();i++) {
			Comanda comanda = (Comanda) modelo.get(i);
			if(comanda.getNumero() == Integer.parseInt(numero)) {
				return (Comanda) modelo.get(i);
			}
		}
		return null;
	}
	
	public void limpa() {
		modelo.clear();
		for(int i = 0; i < numeroComandas;i++) {
			modelo.add(i, new Comanda(i+1)); 
		}
		lastComanda = numeroComandas+1;		
	}
	public void addComanda() {
		modelo.add(new Comanda(++lastComanda));
	}
	
	public void addListSelectionLisetener(ListSelectionListener listener) {
		getLista().addListSelectionListener(listener);
	}
	
	public JList getLista() {
		return lista;
	}
	
	public void abrirComanda(int numero) {
		for(int i=0; i<modelo.getSize();i++) {
			Comanda comanda = (Comanda) modelo.get(i);
			if(comanda.getNumero() == numero) {
				comanda.setAberta(true);
			}
		}
	}
	
	public void setComanda(int numero, Comanda comanda ) {
		for(int i=0; i<modelo.getSize();i++) {
			Comanda c = (Comanda) modelo.get(i);
			if(c.getNumero() == numero) {
				c.setCliente(comanda.getCliente());
				c.setAberta(comanda.isAberta());
			}
		}
	}

	class Render implements ListCellRenderer {

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			if(value instanceof Comanda) {
				Comanda comanda = (Comanda)value;
				ImageIcon image;
				if(comanda.isAberta())
					image = new ImageIcon(SisListComandas.class.getResource("/siscom/icones/red-32.png"));
				else
					image = new ImageIcon(SisListComandas.class.getResource("/siscom/icones/green-32.png"));
				
				JLabel label = new JLabel(String.valueOf(comanda.getNumero()), image, JLabel.HORIZONTAL);
				label.setVerticalTextPosition(JLabel.BOTTOM);
				label.setHorizontalTextPosition(JLabel.CENTER);
				if(isSelected) {
					label.setBorder(new LineBorder(Color.BLACK));
				}
				return label;
			}
			return null;
		}
		
	}
	
	class Modelo extends DefaultListModel {
		/**
		 * 
		 */
		private static final long serialVersionUID = -1022562119050408660L;
		public void add(int indice, Comanda comanda) {
			super.add(indice, comanda);
		}
		public void add(Comanda comanda) {
			super.addElement(comanda);
		}
	}
}
